/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.guobo.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.guobo.entity.WarningRole;
import com.thinkgem.jeesite.modules.guobo.entity.WarnroleRfid;
import com.thinkgem.jeesite.modules.guobo.service.WarnroleRfidService;

/**
 * 预警规则rfid中间表Controller
 * @author wudavid
 * @version 2017-03-09
 */
@Controller
@RequestMapping(value = "${adminPath}/guobo/warnroleRfid")
public class WarnroleRfidController extends BaseController {

	@Autowired
	private WarnroleRfidService warnroleRfidService;
	
	@ModelAttribute
	public WarnroleRfid get(@RequestParam(required=false) String id) {
		WarnroleRfid entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = warnroleRfidService.get(id);
		}
		if (entity == null){
			entity = new WarnroleRfid();
		}
		return entity;
	}
	
	@RequiresPermissions("guobo:warnroleRfid:view")
	@RequestMapping(value = {"list", ""})
	public String list(WarnroleRfid warnroleRfid, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WarnroleRfid> page = warnroleRfidService.findPage(new Page<WarnroleRfid>(request, response), warnroleRfid); 
		model.addAttribute("page", page);
		return "modules/guobo/warnroleRfidList";
	}

	@RequiresPermissions("guobo:warnroleRfid:view")
	@RequestMapping(value = "form")
	public String form(WarnroleRfid warnroleRfid, Model model) {
		model.addAttribute("warnroleRfid", warnroleRfid);
		return "modules/guobo/warnroleRfidForm";
	}

	@RequiresPermissions("guobo:warnroleRfid:edit")
	@RequestMapping(value = "save")
	public String save(WarnroleRfid warnroleRfid, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, warnroleRfid)){
			return form(warnroleRfid, model);
		}
		warnroleRfidService.save(warnroleRfid);
		addMessage(redirectAttributes, "保存预警规则rfid中间表成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/warnroleRfid/?repage";
	}
	
	@RequiresPermissions("guobo:warnroleRfid:edit")
	@RequestMapping(value = "delete")
	public String delete(WarnroleRfid warnroleRfid, RedirectAttributes redirectAttributes) {
		warnroleRfidService.delete(warnroleRfid);
		addMessage(redirectAttributes, "删除预警规则rfid中间表成功");
		return "redirect:"+Global.getAdminPath()+"/guobo/warnroleRfid/?repage";
	}

	/**
	 * ajax方式保存
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:warnroleRfid:edit")
	@ResponseBody
	@RequestMapping(value = "saveWarningRoleRfids")
	public Map<String, Object> saveWarningRoleRfids(Model model,HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		String assetIds = request.getParameter("assetIds");
		String warningRoleId = request.getParameter("warningRoleId");
		String[] tempAssetids = assetIds.split(",");
		warnroleRfidService.saveWarnroleRfids(warningRoleId, tempAssetids);
		data.put("code", "SUCCESS");
		return data;
	}
	
	/**
	 * ajax方式保存
	 * @param response
	 * @return
	 */
	@RequiresPermissions("guobo:warnroleRfid:edit")
	@ResponseBody
	@RequestMapping(value = "deleteWarningRoleRfid")
	public Map<String, Object> deleteWarningRoleRfid(WarnroleRfid warnroleRfid,Model model,HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		warnroleRfidService.deleteInfoByRfidRole(warnroleRfid);;
		data.put("code", "SUCCESS");
		return data;
	}
}